<template>
  <view class="comBox">
    <sickTitleCom></sickTitleCom>
    <view class="body">
      <view class="titleBold">您的父母、子女、兄弟姐妹是否患过以下疾病（可多选）:</view>
      <view class="sickList">
        <view
          v-for="item in familySickList"
          :key="item.id"
          :class="[item.active ? 'sickSpan cur' : 'sickSpan']"
          @click="choseList(item)"
        >
          {{ item.name }}
          <span v-if="item.active" class="curImg"><img :src="curIcon" /></span>
        </view>
        <view :class="[noChose ? 'sickSpan cur' : 'sickSpan', 'rowBlock']" @click="clearChose">
          均未患上述疾病
          <span v-if="noChose" class="curImg"><img :src="curIcon" /></span>
        </view>
      </view>
      <!-- 下一题上一题按钮 -->
      <nextProBtnCom @nextBtn="nextBtn" @proBtn="proBtn"></nextProBtnCom>
    </view>
  </view>
</template>

<script>
import { getAssessDiseases } from '@/api/assessQuestion';
import nextProBtnCom from '@/components/nextProButton';
import sickTitleCom from './sickTitleCom';
import { ajaxSend } from './ajaxHealth.js';
import curIcon from '@/static/icon/cur.png';
import { mapGetters } from 'vuex';
export default {
  components: {
    // 上一题下一题组件
    nextProBtnCom,
    sickTitleCom
  },
  data() {
    return {
      mainData: {},
      familySickList: [],
      curIcon: curIcon,
      noChose: false
    };
  },
  computed: {
    ...mapGetters(['active', 'assessForm'])
  },
  mounted() {
    this.getDiseases();
  },
  methods: {
    async nextBtn() {
      // 疾病直接提交接口
      let ownData = [];
      let icd10Item = [];
      this.familySickList.forEach((item) => {
        if (item.active) {
          let obj = {
            diseaseId: item.icd10,
            relation: 17
          };
          icd10Item.push(item.icd10);
          ownData.push(obj);
        }
      });
      if (ownData.length < 1 && !this.noChose) {
        uni.showToast({ title: `请选择是否患病`, icon: 'none' });
        return false;
      }
      // 获取健康指标指导意见 判断是出健康提示还是直接下一步 start
      const healthData = await ajaxSend({ homeMedicalHistories: icd10Item });
      if (healthData && healthData.length > 0) {
        // 如果有异常，则提示
        this.$store.commit('assessQuestion/SET_AJAXRESMESSAGE', healthData); // 存提示信息到vux，数组格式
        this.$store.commit('assessQuestion/SET_AJAXRES', true); // 开启提示
      } else {
        let curActive = this.active + 1;
        this.$store.commit('assessQuestion/SET_ACTIVE', curActive);
      }
      this.$store.commit('assessQuestion/SET_FAMILYSICKLIST', ownData);
      // 获取健康指标指导意见 判断是出健康提示还是直接下一步 end
    },
    proBtn() {
      let curActive = this.active - 1;
      this.$store.commit('assessQuestion/SET_ACTIVE', curActive);
    },
    getDiseases() {
      getAssessDiseases().then((res) => {
        if (res.code === 0) {
          let main = res.data.family;
          main.forEach((item, index) => {
            main[index].active = false;
          });
          this.familySickList = main;
        }
      });
    },
    choseList(val) {
      this.noChose = false;
      const indexs = this.familySickList.findIndex((item) => item.id === val.id);
      this.familySickList[indexs].active = !this.familySickList[indexs].active;
      // this.noChose = !this.familySickList.some((item) => item.active);
    },
    clearChose() {
      if (this.noChose) {
        this.noChose = false;
        return;
      }
      this.familySickList.forEach((item, index) => {
        this.familySickList[index].active = false;
      });
      this.noChose = true;
    }
  }
};
</script>
<style lang="scss">
@import './../style/sickOwnFam.scss';
</style>
